<?php 
class Cart{
	
	
	
	private $product_id_rule = '\.a-z0-9-_';    //小写字母 | 数字 | ._-  
    private $product_name_rule = '\.\:a-z0-9-_';//小写字母 | 数字 | ._-:  
    private $debug = TRUE;  
	private $_cart_contents = array();
	
	
	public function __construct(){
		if(isset($_SESSION['cart_contents'])){
			$this->_cart_contents = $_SESSION['cart_contents'];
		}else {
			$this->_cart_contents['cart_total']=0;
			$this->_cart_contents['total_items']=0;
		}
		
		if($this->debug ===TRUE){
			//$this->_log("");
		}
	}
	
	public function insert($items = array()){
		
		if (!is_array($items) OR count($items)==0){
			if($this->debug === TRUE){
				$this->_log("cart_no_items_insert");
			}
			return FALSE;
		}
		
		$save_cart = FALSE;
		if(isset($items['id'])){
			if ($this->_insert($items)===TRUE){
				$save_cart = TRUE;
			}
		} else {
			foreach ($items as $val){
				if(is_array($val) AND isset($val['id'])){
					if ($this->_insert($val) == TRUE) {
						$save_cart = TRUE;
					}
				}
			}
		}
		
		if($save_cart){
			$this->_save_cart();
			return TRUE;
		}
		
		return FALSE;
	}
	
	public function update($items = array()) {  
         //输入物品参数异常  
         if( !is_array($items) OR count($items) == 0) {  
             if($this->debug === TRUE) {  
                 $this->_log("cart_no_items_insert");  
             }  
             return FALSE;  
         }
         //物品参数处理  
         $save_cart = FALSE;  
         if(isset($items['rowid']) AND isset($items['quantity'])) {  
             if($this->_update($items) === TRUE) {  
                $save_cart = TRUE;  
             }  
         } else {  
             foreach($items as $val) {  
                 if(is_array($val) AND isset($val['rowid']) AND isset($val['quantity'])) {  
                     if($this->_update($val) === TRUE) {  
                         $save_cart = TRUE;  
                     }  
                 }  
             }  
         }  
         //当更新成功后保存数据到session  
         if($save_cart) {  
             $this->_save_cart();  
             return TRUE;  
         }
         return FALSE;  
     }
	
	public function total(){
		return $this->_cart_contents['cart_total'];
	}
	
	public function total_items(){
		return $this->_cart_contents['total_items'];
	}
	
	public function contents() {  

         return $this->_cart_contents;  

     } 
	
	public function options($rowid = ''){
		if($this->has_options($rowid)){
			return $this->_cart_contents[$rowid]['options'];
		} else {
			return array();
		}
	}
	
	public function destroy(){
		unset($this->_cart_contents);
		
		$this->_cart_contents['cart_total']=0;
		$this->_cart_contents['total_items']=0;
		
		unset($_SESSION['cart_contents']);
		unset($_SESSION['cart_total']);
		unset($_SESSION['transportation']);
		unset($_SESSION['daijinjuan_total_init']);
		unset($_SESSION['daijinjuan_total']);
		unset($_SESSION['total']);
		unset($_SESSION['weight_total']);
		
		
	}
	
	public function del($id){
		$id=md5($id);
		unset($_SESSION[$id]);
	}
	
	private function has_option($rowid = ''){
		if(!isset($this->_cart_contents[$rowid]['options']) OR count($this->_cart_contents[$rowid]['options']===0)){
			return FALSE;
		}
		
		return TRUE;
	}
	
	private function _insert($items = array()){
		if(!is_array($items) OR count($items)==0){
			if ($this->debug === TRUE){
				$this->_log("cart_no_data_insert");
			}
			return FALSE;
		}
		
		if (!isset($items['id']) OR !isset($items['quantity']) OR !isset($items['price']) OR !isset($items['name'])){
			if($this->debug === TRUE){
				$this->_log("cart_items_data_invalid");
			}
			return FALSE;
		}
		
		$items['quantity'] = trim(preg_replace('/([^0-9])/i', '', $items['quantity']));
		$items['quantity'] = trim(preg_replace('/^([0]+)/i', '', $items['quantity']));
		
		if( ! is_numeric($items['quantity']) OR $items['quantity'] == 0) {
			if($this->debug === TRUE) {
				$this->_log("cart_items_data(quantity)_invalid");
			}
			return FALSE;
		}
		
		if( ! preg_match('/^['.$this->product_id_rule.']+$/i', $items['id'])) {
			if($this->debug === TRUE) { 
				$this->_log("cart_items_data(id)_invalid");
			}
			return FALSE; 
		}
		
/*		if( ! preg_match('/^['.$this->product_name_rule.']+$/i', $items['name'])) {
			if($this->debug === TRUE) { 
				$this->_log("cart_items_data(name)_invalid");
			}
			return FALSE;
		}*/
		
		$items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
		$items['price'] = trim(preg_replace('/^([0]+)/i', '', $items['price'])); 
		
		if( ! is_numeric($items['price'])) {
			if($this->debug === TRUE) { 
				$this->_log("cart_items_data(price)_invalid"); 
			}
			return FALSE;
		}
		
		if(isset($items['options']) AND count($items['options']) >0) {
			$rowid = md5($items['id'].implode('', $items['options'])); 
		}else {
			$rowid = md5($items['id']);
		}
		
		unset($this->_cart_contents[$rowid]); 
		$this->_cart_contents[$rowid]['rowid'] = $rowid;
		foreach($items as $key => $val) { 
			$this->_cart_contents[$rowid][$key] = $val; 
		}
		return TRUE;
		
	}
	
	private function _update($items = array()) { 
		if( ! isset($items['rowid']) OR ! isset($items['quantity']) OR ! isset($this->_cart_contents[$items['rowid']])) { 
			if($this->debug == TRUE) { 
				$this->_log("cart_items_data_invalid");
			}
			return FALSE;
		}
		
		$items['quantity'] = preg_replace('/([^0-9])/i', '', $items['quantity']);
		$items['quantity'] = preg_replace('/^([0]+)/i', '', $items['quantity']); 
		
		if( ! is_numeric($items['quantity'])) { 
			if($this->debug === TRUE) { 
				$this->_log("cart_items_data(quantity)_invalid");
			}
			return FALSE;
		}
		
		if($this->_cart_contents[$items['rowid']]['quantity'] == $items['quantity']) { 
			if($this->debug === TRUE) { 
				$this->_log("cart_items_data(quantity)_equal"); 
			}
			return FALSE;
		}
		
		if($items['quantity'] == 0) {
			unset($this->_cart_contents[$items['rowid']]); 
		}else {
			$this->_cart_contents[$items['rowid']]['quantity'] = $items['quantity'];
		}
		return TRUE; 
	}
	
	private function _save_cart() {
		unset($this->_cart_contents['total_items']);
		unset($this->_cart_contents['cart_total']); 
		
		$total = 0; 
		foreach($this->_cart_contents as $key => $val) {
			if( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['quantity'])) { 
				continue; 
			}
			$total += ($val['price'] * $val['quantity']); 
			$daijinjuan_total += ($val['daijinjuan'] * $val['quantity']);
			$weight += ($val['weight'] * $val['quantity']);
			$this->_cart_contents[$key]['subtotal'] = ($val['price'] * $val['quantity']); 
			
		}
		
		$this->_cart_contents['total_items'] = count($this->_cart_contents); 
		$this->_cart_contents['cart_total'] = $total;
		$this->_cart_contents['daijinjuan_total'] =$daijinjuan_total;
		$this->_cart_contents['weight_total'] =$weight;
			
		if(count($this->_cart_contents) <= 2) { 
			unset($_SESSION['cart_contents']); 
			return FALSE; 
		}
		$_SESSION['cart_contents'] = $this->_cart_contents; 
		return TRUE;
	}
	
	private function _log($msg) { 
		return @file_put_contents('cart_err.log', $msg, FILE_APPEND); 
	}
	
	
}

?>